package com.example.sxproject.controller;

import com.example.sxproject.pojo.*;
import com.example.sxproject.service.*;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

//此controller存放所有页面跳转
//通过访问路由，然后跳转到对应html
@Controller
public class indexController {

    @Autowired
    private UserService userService;

    @Autowired
    private CardService cardService;

    @Autowired
    private FinancialService financialService;

    @Autowired
    private RecipientService recipientService;

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private  CompanyService companyService;

    @Autowired
    private  LoanService loanService;

    //跳转登录界面login.html
    @RequestMapping("/main")
    public String main() {
        return "login";
    }

    //跳转登录界面login.html
    @RequestMapping("/login")
    public String login(HttpSession session) {
        session.setAttribute("msg", "");
        return "login";
    }

    //跳转注册界面register.html
    @RequestMapping("/register")
    public String register(HttpSession session) {
        session.setAttribute("msg", "");
        return "register";
    }

    //跳转主页-收支界面money.html
    @RequestMapping("/money")
    public String money(HttpSession session) {
        User user = (User) session.getAttribute("user"); //获取session中的信息，并强制转化成User类型
        List<Payment> payments = paymentService.getPaymentsByUserId(user.getId());// 返回全部支付记录
        session.setAttribute("payments", payments); // 存到session中
        return "money";
    }

    //跳转主页-信用卡card.html
    @RequestMapping("/card")
    public String card(HttpSession session) {

        User user = (User) session.getAttribute("user"); //获取session中的信息，并强制转化成User类型

        if (user.getRealName().isEmpty() || user.getRealName() == " ") { //判断用户信息是否为空
            session.setAttribute("msg", "请先完善个人信息!");
            return "myInformation";
        } else {
            session.setAttribute("msg", "");
        }

        List<Card> cards = cardService.getCardsByUserId(user.getId());// 根据用户id查找所有银行卡
        session.setAttribute("cards", cards);

        return "card";
    }

    //跳转主页-转账card.html
    @RequestMapping("/recipientList")
    public String recipientList(HttpSession session) {
        User user = (User) session.getAttribute("user"); //获取session中的信息，并强制转化成User类型
        List<Recipient> recipients = recipientService.selectByUserId(user.getId());
        session.setAttribute("recipients", recipients);
        return "recipientList";
    }

    //跳转生活缴费address.html
    @RequestMapping("/address")
    public String address(HttpSession session) {

        List<Company> companies = companyService.selectAll();
        session.setAttribute("companies", companies);

        return "address";
    }

    //跳转主页-生活缴费pay.html
    @RequestMapping("/pay")
    public String pay() {
        return "pay";
    }

    //跳转主页index.html
    @RequestMapping("/index")
    public String index(HttpSession session) {

        User user = (User) session.getAttribute("user"); //获取session中的信息，并强制转化成User类型

        if (user.getRealName().isEmpty() || user.getRealName() == " ") { //判断用户信息是否为空
            session.setAttribute("msg", "请先完善个人信息!");
            return "myInformation";
        } else {
            session.setAttribute("msg", "");
        }
        return "index";
    }

    //跳转个人界面myInformation.html
    @RequestMapping("/myInformation")
    public String myInformation(HttpSession session) {

        User user = (User) session.getAttribute("user"); //获取session中的信息，并强制转化成User类型
        List<Card> cards = cardService.getCardsByUserId(user.getId());// 根据用户id查找所有银行卡

        session.setAttribute("cards", cards);
        int cardSum = 0;
        for(Card card:cards) {
            if (card.getStatusId() == 1 || card.getStatusId() == 2) {
                cardSum++;
            }
        }
        session.setAttribute("cardSum", cardSum);

        double moneySum = 0;
        double debt = 0;

        for (Card card : cards) {
            moneySum += card.getBalance();
            debt += card.getRepaid();
        }

        session.setAttribute("moneySum", moneySum);// 存下总资产
        session.setAttribute("debt", debt); //存下总还款额

        if (user.getRealName().isEmpty() || user.getRealName() == " ") { //判断用户信息是否为空
            session.setAttribute("msg", "请先完善个人信息!");
            return "myInformation";
        } else {
            session.setAttribute("msg", "");
        }

        return "myInformation";
    }

    //跳转金融助手finance.html
    @RequestMapping("/finance")
    public String finance(HttpSession session) {

        User user = (User) session.getAttribute("user"); //获取session中的信息，并强制转化成User类型
        System.out.println("userName = " + user.getRealName());
        if (user.getRealName().isEmpty() || user.getRealName() == " ") { //判断用户信息是否为空
            session.setAttribute("msg", "请先完善个人信息!");
            return "myInformation";
        } else {
            session.setAttribute("msg", "");
        }
        return "finance";
    }

    //跳转挂失界面cardSet.html
    @RequestMapping("/cardSet")
    public String cardSet(HttpSession session, String cardId) {
        session.setAttribute("cardId", cardId);
        session.setAttribute("msg", "");
        return "cardSet";
    }

    //跳转收款人修改界面/editRecipient.html
    @RequestMapping("/editRecipient")
    public String editRecipient(HttpSession session) {

        return "editRecipient";
    }

    //跳转收款人详情transferAssure.html,还有2个参数
    @RequestMapping("/transferAssure")
    public String transferAssure(HttpSession session) {
        User user = (User) session.getAttribute("user"); //获取session中的信息，并强制转化成User类型

        List<Card> cards = cardService.getCardsByUserId(user.getId());

        session.setAttribute("cards", cards);
        session.setAttribute("msg", "");
        return "transferAssure";
    }

    //跳转账户列表transferAssure.html
    @RequestMapping("/accountList")
    public String accountList() {
        return "accountList";
    }

    //新增账户列表accountListForSelect.html
    @RequestMapping("/accountListForSelect")
    public String accountListForSelect() {
        return "accountListForSelect";
    }

    //跳转交易记录transrecord.html
    @RequestMapping("/transrecord")
    public String transrecord() {
        return "transrecord";
    }

    //修改个人信息changedInformation.html
    @RequestMapping("/changedInformation")
    public String changedInformation(HttpSession session) {
        return "changedInformation";
    }

    //transferOp.html
    @RequestMapping("/transferOp")
    public String transferOp() {
        return "transferOp";
    }

    //对外转账transferManage.html
    @RequestMapping("/transferManage")
    public String transferManage() {
        return "transferManage";
    }

    //对外转账recipientListForSelect.html
    @RequestMapping("/recipientListForSelect")
    public String recipientListForSelect() {
        return "recipientListForSelect";
    }

    //转账确认payment.html
    @RequestMapping("/payment")
    public String payment(HttpSession session, String companyCount) {

        User user = (User) session.getAttribute("user"); //获取session中的信息，并强制转化成User类型

        List<Card> cards = cardService.getCardsByUserId(user.getId());// 根据用户id查找所有银行卡
        session.setAttribute("cards", cards);

        Company company = companyService.findByCount(companyCount);
        session.setAttribute("company", company);
        session.setAttribute("msg", "");
        return "payment";
    }

    //成功success.html
    @RequestMapping("/success")
    public String success() {
        return "success";
    }

    //信用卡申请credit_card_application.html
    @RequestMapping("/credit_card_application")
    public String credit_card_application(HttpSession session) {

        return "credit_card_application";
    }

    //信用卡评估credit_assessment.html
    @RequestMapping("/credit_assessment")
    public String credit_assessment() {
        return "credit_assessment";
    }

    //贷款办理loan_application.html
    @RequestMapping("/loan_application")
    public String loan_application() {
        return "loan_application";
    }

    //汇率计算exchange_rate.html
    @RequestMapping("/exchange_rate")
    public String exchange_rate() {
        return "exchange_rate";
    }

    //理财产品financeProduct.html
    @RequestMapping("/financeProduct")
    public String financeProduct(HttpSession session) {
        List<FinancialProduct> financialProducts = financialService.SelectAll();
        session.setAttribute("financialProducts", financialProducts);
        return "financeProduct";
    }

    //跳转贷款信息界面
    @RequestMapping("/loanProduct")
    public String loanProduct(HttpSession session) {
        List<LoanProduct> products = loanService.SelectAll();
        session.setAttribute("loanProducts", products);
        return "loanProduct";
    }

    //理财产品financeProduct.html
    @RequestMapping("/financeProductdetails")
    public String financeProductdetails(HttpSession session, Integer productId) {

        User user = (User) session.getAttribute("user");
        List<Card> cards = cardService.getCardsByUserId(user.getId());
        FinancialProduct product = financialService.findByProductId(productId);

        session.setAttribute("product", product);
        session.setAttribute("cards", cards);

        return "financeProductdetails";
    }

    //跳转贷款详情
    @RequestMapping("/loanProductdetails")
    public String loanProductdetails(HttpSession session, Integer productId) {

        User user = (User) session.getAttribute("user");
        LoanProduct product = loanService.SelectByProductId(productId);
        session.setAttribute("loanProduct", product);

        return "loanProductdetails";
    }

    @RequestMapping("/myProduct")
    public String myProduct(HttpSession session) {
        User user = (User) session.getAttribute("user");

        List<FinancialUser> financialProducts = financialService.findByUserId(user.getId());
        List<LoanUser> loanProducts = loanService.findByUserId(user.getId());

        session.setAttribute("financialProducts", financialProducts);
        session.setAttribute("loanProducts", loanProducts);

        return "myProduct";
    }

    //跳转支付界面
    @RequestMapping("/transMoney")
    public String transMoney(HttpSession session) {
        User user = userService.findByPhone("17613157388");
        Card card = cardService.findByIdNumber("111649821237");
        session.setAttribute("userTest", user);
        session.setAttribute("card", card);
        return "transferAssure";
    }


    //用户信息查询
    @RequestMapping("/message")
    public String message(){
        return "userMessage";
    }

    //管理员登陆
    @RequestMapping("/mlogin")
    public String mlogin(){
        return "mlogin";
    };

    //用户信息添加
    @RequestMapping("/addUser")
    public String addUser(){return "addUserMessage";};

    //用户信息更新
    @RequestMapping("userUpdate")
    public String userUpdate(){return "userMessageUpdate";};

    //用户信息假删除
    @RequestMapping("userDel")
    public String deleteUser(){return "deleteUser";}

    //管理员交易记录查询
    @RequestMapping("/pmessage")
    public String pmessage(){return "paymentMessage";}

    //交易记录修改
    @RequestMapping("/paymentUpdate")
    public String paymentUpdate(){return "paymentMessageUpdate";}

    //交易记录假删除
    @RequestMapping("pdel")
    public  String pdel(){return "paymentMessageDel";}

    //管理员理财产品查询
    @RequestMapping("fmessage")
    public String fmessage(){
        return "financeMessage";
    }

    //管理员理财产品添加
    @RequestMapping("fadd")
    public String fadd(){return "financeAdd";}

    //管理员查询被用户购买的理财产品
    @RequestMapping("fumessage")
    public String fumessage(){return "financialUserMessage";}

    //银行卡信息
    @RequestMapping("cmessage")
    public String cmessage(){return "cardMessage";}

    //银行卡申请信息
    @RequestMapping("crmessage")
    public String crmessage(){return "cardReviewMessage";}

    //银行卡开户
    @RequestMapping("crupdate")
    public String crupdate(){
        return "cardReviewUpdate";
    }

    //银行卡驳回
    @RequestMapping("cardBoHui")
    public String cardBoHui(){return "cardNoReview";}

    //公司信息
    @RequestMapping("companymessage")
    public String companymessage(){return "companyMessage";}

    //公司信息编辑
    @RequestMapping("UpdateCompany")
    public String UpdateCompany(){return "companyUpdate";}

    //管理员注销公司
    @RequestMapping("companydel")
    public String companydel(){return "companyDel";}

    //公司添加
    @RequestMapping("companymessageadd")
    public String companyadd(){return "companyAdd";}

    //贷款产品查询
    @RequestMapping("loanproductmessage")
    public String loanproductmessage(){return "loanMessage";}

    //增加贷款产品
    @RequestMapping("loanproductadd")
    public String loanproductadd(){return "loanProductAdd";}

    //贷款产品信息更新
    @RequestMapping("loanproductupdate")
    public String loanproductupdate(){return "loanProductUpdate";}

    //用户贷款信息
    @RequestMapping("loanusermessage")
    public String loanusermessage(){return "loanUserMessage";}

    //用户贷款申请
    @RequestMapping("loanapply")
    public String loanapply(){return "loanUserApplyMessage";}

    @RequestMapping("pass")
    public String pass(){return "loanUserApplyPass";}


    @RequestMapping("nopass")
    public String nopass(){return "loanUserApplyNoPass";}

}
